package com.app.service.Impl;

import com.app.common.Result;
import com.app.mapper.ActivityEnlistMapper;
import com.app.mapper.ActivityMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.service.ActivityService;
import com.app.util.CommonHelp;
import com.app.vo.ActivityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityEnlistMapper activityEnlistMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public ActivityVo selectActivityList(Integer type, String title, Integer page, Integer limit, Integer villageId) throws ParseException {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
        if (title != null && !title.equals("")) {
            List<ActivityModel> activityModels = activityMapper.selectActivityByTitle(title, page, limit, villageId);
            if (!activityModels.isEmpty()) {
                for (int i = 0; i < activityModels.size(); i++) {
                    Integer integer = selectEnrollment(activityModels.get(i).getId());
                    if (integer == null) {
                        activityModels.get(i).setEnrollment(0);
                    }
                    activityModels.get(i).setEnrollment(integer);

                }
            }
            for (int i = 0; i < activityModels.size(); i++) {
                if (activityModels.get(i).getStartTime() != null && !activityModels.get(i).getStartTime().equals("")
                        && activityModels.get(i).getEndTime() != null && !activityModels.get(i).getEndTime().equals("")) {
                    //开始时间
                    Calendar start = Calendar.getInstance();
                    Date startTime = simpleDateFormat.parse(activityModels.get(i).getStartTime());//String转Date
                    start.setTime(startTime);
                    Calendar end = Calendar.getInstance();
                    Date endTime = simpleDateFormat.parse(activityModels.get(i).getEndTime());//String转Date
                    end.setTime(endTime);
                    Calendar now = Calendar.getInstance();
                    String nowTime = simpleDateFormat.format(new Date());//当前时间格式化
                    Date now1 = simpleDateFormat.parse(nowTime);//String转Date
                    now.setTime(now1);
                    if (now.before(start)) {
                        //未开始
                        activityModels.get(i).setState(1);
                        activityModels.get(i).setStateText("未开始");
                    }
                    if (now.after(start) && now.before(end)) {
                        //进行中
                        activityModels.get(i).setState(2);
                        activityModels.get(i).setStateText("进行中");
                    }
                    if (now.after(end)) {
                        //已结束
                        activityModels.get(i).setState(3);
                        activityModels.get(i).setStateText("已结束");
                    }
                }
            }
            QueryWrapper<ActivityModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("title", title);
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = activityMapper.selectCount(queryWrapper);
            ActivityVo vo = new ActivityVo();
            vo.setList(activityModels);
            vo.setCount(count);
            return vo;
        }
        if (type != null && type == 0) {
            List<ActivityModel> activityModels = activityMapper.selectActivityList(page, limit, villageId);
            if (!activityModels.isEmpty()) {
                for (int i = 0; i < activityModels.size(); i++) {
                    Integer integer = selectEnrollment(activityModels.get(i).getId());
                    if (integer == null) {
                        activityModels.get(i).setEnrollment(0);
                    }
                    activityModels.get(i).setEnrollment(integer);
                }
            }
            for (int i = 0; i < activityModels.size(); i++) {
                if (activityModels.get(i).getStartTime() != null && !activityModels.get(i).getStartTime().equals("")
                        && activityModels.get(i).getEndTime() != null && !activityModels.get(i).getEndTime().equals("")) {
                    //开始时间
                    Calendar start = Calendar.getInstance();
                    Date startTime = simpleDateFormat.parse(activityModels.get(i).getStartTime());//String转Date
                    start.setTime(startTime);
                    Calendar end = Calendar.getInstance();
                    Date endTime = simpleDateFormat.parse(activityModels.get(i).getEndTime());//String转Date
                    end.setTime(endTime);
                    Calendar now = Calendar.getInstance();
                    String nowTime = simpleDateFormat.format(new Date());//当前时间格式化
                    Date now1 = simpleDateFormat.parse(nowTime);//String转Date
                    now.setTime(now1);
                    if (now.before(start)) {
                        //未开始
                        activityModels.get(i).setState(1);
                        activityModels.get(i).setStateText("未开始");
                    }
                    if (now.after(start) && now.before(end)) {
                        //进行中
                        activityModels.get(i).setState(2);
                        activityModels.get(i).setStateText("进行中");
                    }
                    if (now.after(end)) {
                        //已结束
                        activityModels.get(i).setState(3);
                        activityModels.get(i).setStateText("已结束");
                    }
                }
            }
            Integer count;
            QueryWrapper<ActivityModel> queryWrapper = new QueryWrapper<>();
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
                count = activityMapper.selectCount(queryWrapper);
            } else {
                count = activityMapper.selectCount(null);
            }
            ActivityVo vo = new ActivityVo();
            vo.setList(activityModels);
            vo.setCount(count);
            return vo;
        } else {
            List<ActivityModel> activityModels = activityMapper.selectActivityListByType(page, limit, type, villageId);
            if (!activityModels.isEmpty()) {
                for (int i = 0; i < activityModels.size(); i++) {
                    Integer integer = selectEnrollment(activityModels.get(i).getId());
                    if (integer == null) {
                        activityModels.get(i).setEnrollment(0);
                    }
                    activityModels.get(i).setEnrollment(integer);
                }
            }
            for (int i = 0; i < activityModels.size(); i++) {
                if (activityModels.get(i).getStartTime() != null && !activityModels.get(i).getStartTime().equals("")
                        && activityModels.get(i).getEndTime() != null && !activityModels.get(i).getEndTime().equals("")) {
                    //开始时间
                    Calendar start = Calendar.getInstance();
                    Date startTime = simpleDateFormat.parse(activityModels.get(i).getStartTime());//String转Date
                    start.setTime(startTime);
                    Calendar end = Calendar.getInstance();
                    Date endTime = simpleDateFormat.parse(activityModels.get(i).getEndTime());//String转Date
                    end.setTime(endTime);
                    Calendar now = Calendar.getInstance();
                    String nowTime = simpleDateFormat.format(new Date());//当前时间格式化
                    Date now1 = simpleDateFormat.parse(nowTime);//String转Date
                    now.setTime(now1);
                    if (now.before(start)) {
                        //未开始
                        activityModels.get(i).setState(1);
                        activityModels.get(i).setStateText("未开始");
                    }
                    if (now.after(start) && now.before(end)) {
                        //进行中
                        activityModels.get(i).setState(2);
                        activityModels.get(i).setStateText("进行中");
                    }
                    if (now.after(end)) {
                        //已结束
                        activityModels.get(i).setState(3);
                        activityModels.get(i).setStateText("已结束");
                    }
                }
            }
            QueryWrapper<ActivityModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", type);
            if (villageId != null && villageId != 0) {
                queryWrapper.eq("village_id", villageId);
            }
            Integer count = activityMapper.selectCount(queryWrapper);
            ActivityVo vo = new ActivityVo();
            vo.setList(activityModels);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public List<SystemModel> selectTypeList() {
        return activityMapper.selectTypeList();
    }

    @Override
    public ActivityModel selectContentById(Integer id) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        ActivityModel model = activityMapper.selectById(id);
        if (model != null) {
            Integer integer = selectEnrollment(model.getId());
            if (integer == null) {
                model.setEnrollment(0);
            }
            model.setEnrollment(integer);
        }
        if (model != null) {
            if (model.getStartTime() != null && model.getEndTime() != null) {
                Date parse = simpleDateFormat.parse(model.getStartTime());
                Date parse1 = simpleDateFormat.parse(model.getEndTime());
                String startTime = simpleDateFormat.format(parse);
                String endTime = simpleDateFormat.format(parse1);
                model.setStartTime(startTime);
                model.setEndTime(endTime);
            }
        }
        return model;
    }

    @Override
    public void application(UserModel user, Integer activityId) {
        ActivityEnlistModel model = ActivityEnlistModel.builder()
                .activityId(activityId)
                .userId(user.getId())
                .name(user.getName())
                .phone(user.getPhoneNumber())
                .idCard(user.getIdCard())
                .status(0)
                .auditStatus(0)
                .createTime(CommonHelp.getNowTimestamp())
                .build();
        activityEnlistMapper.insert(model);
    }


    @Override
    public Result upload(String files, Integer id, Integer activityId) {
        QueryWrapper<ActivityEnlistModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        List<ActivityEnlistModel> activityEnlistModels = activityEnlistMapper.selectList(queryWrapper);
        for (int i = 0; i < activityEnlistModels.size(); i++) {
            if (activityEnlistModels.get(i) == null) {
                return Result.error(500, "没报名");
            }
            if (activityEnlistModels.get(i).getActivityId().equals(activityId)) {
                activityEnlistModels.get(i).setFiles(files);
                activityEnlistModels.get(i).setAuditStatus(0);
                activityEnlistMapper.updateById(activityEnlistModels.get(i));
            }
        }
        return null;
    }

    @Override
    public ActivityEnlistModel selectFiles(Integer activityId, Integer userId) {
        QueryWrapper<ActivityEnlistModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("user_id", userId);
        return activityEnlistMapper.selectAudit(activityId, userId);
    }

    private Integer selectEnrollment(Integer activityId) {
        return activityEnlistMapper.selectEnrollment(activityId);
    }
}
